package cn.wit.summit.chwan.adapters;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;
import java.util.List;

import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.ui.activity.ImagePagerActivity;
import cn.wit.summit.chwan.utils.MyImageLoader;

public class GameDetailImgAdapter extends RecyclerView.Adapter {
    List<String> strings = new ArrayList<>();
    Context context;
    int pic_position;
    public GameDetailImgAdapter(List<String> strings, Context context,int pic_position) {
        this.context = context;
        this.strings = strings;
        this.pic_position = pic_position;
    }

    private class ViewHolder extends RecyclerView.ViewHolder {
        SimpleDraweeView imageViewVPic;
        SimpleDraweeView imageViewHPic;

        public ViewHolder(View itemView) {
            super(itemView);
        }
//        ImageView imageViewPlay;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ViewHolder holder;
     View    convertView = LayoutInflater.from(context).inflate(R.layout.game_detail_item, null);

        holder = new ViewHolder(convertView);
        holder.imageViewVPic = (SimpleDraweeView) convertView
                .findViewById(R.id.imageViewVPic);
        holder.imageViewHPic = (SimpleDraweeView) convertView
                .findViewById(R.id.imageViewHPic);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holderx, final int position) {
        ViewHolder holder=(ViewHolder) holderx;
        if (pic_position == 1) {
            holder.imageViewVPic.setVisibility(View.VISIBLE);
            holder.imageViewHPic.setVisibility(View.GONE);
            MyImageLoader.load(  holder.imageViewVPic,strings.get(position));
        } else {
            holder.imageViewHPic.setVisibility(View.VISIBLE);
            holder.imageViewVPic.setVisibility(View.GONE);
            MyImageLoader.load(holder.imageViewHPic, strings.get(position));
        }

        holder.imageViewHPic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    Intent intent = new Intent(v.getContext(), ImagePagerActivity.class);
                    // 图片url,为了演示这里使用常量，一般从数据库中或网络中获取
                    String[] urls = new String[strings.size()];
                    for (int i = 0; i < strings.size(); i++) {
                        urls[i] = strings.get(i);
                    }
                    intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, urls);
                    intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
                   v .getContext().startActivity(intent);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }); holder.imageViewVPic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    Intent intent = new Intent(v.getContext(), ImagePagerActivity.class);
                    // 图片url,为了演示这里使用常量，一般从数据库中或网络中获取
                    String[] urls = new String[strings.size()];
                    for (int i = 0; i < strings.size(); i++) {
                        urls[i] = strings.get(i);
                    }
                    intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, urls);
                    intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
                    v.getContext().startActivity(intent);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return strings.size();
    }
}
